package com.eat.bluetooth4;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "qwe";
    private BluetoothAdapter mBluetoothAdapter;
    private BluetoothReceiver mBluetoothReceiver;
    private Button openBT,setShowTime,scanBT;
    private TextView localBTMsg;
    private ListView hadPairBTMSG,noPairBTMSG;

    public List<String> noPairBTM;
    public List<String> hadPairBTM;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //开启蓝牙按钮
        openBT = (Button) findViewById(R.id.openBT);
        openBT.setOnClickListener(new MyOpenBT());
        //本机蓝牙信息
        localBTMsg = (TextView) findViewById(R.id.localBTMsg);
        //已配对蓝牙信息
        hadPairBTMSG = (ListView) findViewById(R.id.hadPairBTMSG);
        //未配对蓝牙信息
        noPairBTMSG = (ListView) findViewById(R.id.noPairBTMSG);
        //设置可见性按钮
        setShowTime = (Button) findViewById(R.id.setShowTime);
        setShowTime.setOnClickListener(new MySetShowListener());
        //开始扫描按钮
        scanBT = (Button) findViewById(R.id.scanBT);
        scanBT.setOnClickListener(new MyScanBT());
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        mBluetoothReceiver = new BluetoothReceiver();

        //判断设备是否拥有蓝牙模块
        if (mBluetoothAdapter == null) {
            Toast.makeText(this, "设备没有蓝牙模块", Toast.LENGTH_LONG).show();
            return;
        }
    }

    @Override
    protected void onStart() {
        //获取本机的蓝牙设备信息
        String localName = mBluetoothAdapter.getName();
        String localAddress = mBluetoothAdapter.getAddress();
        localBTMsg.setText("本机的蓝牙名字是：" + localName + "，地址是：" + localAddress + "。");
        super.onStart();
    }

    @Override
    protected void onResume() {
        //获取已配对的远程蓝牙设备的信息
        hadPairBTM = new ArrayList<String>();
        Set<BluetoothDevice> devices = mBluetoothAdapter.getBondedDevices();
        for (Iterator iterator = devices.iterator(); iterator.hasNext();) {
            BluetoothDevice bluetoothDevice = (BluetoothDevice) iterator.next();
            hadPairBTM.add("已配对的蓝牙设备名字是：" + bluetoothDevice.getName() + "，地址是：" + bluetoothDevice.getAddress() + "。");
        }
        ArrayAdapter<String> arrayHadList = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1, hadPairBTM);
        hadPairBTMSG.setAdapter(arrayHadList);
        Log.d(TAG, "on1");

        //注册广播
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        registerReceiver(mBluetoothReceiver, filter);
        Log.d(TAG, "on");

        filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        registerReceiver(mBluetoothReceiver, filter);
        super.onResume();
    }

    private class MyOpenBT implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            if (!mBluetoothAdapter.isEnabled()) {
                //开启蓝牙
                Intent openIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivity(openIntent);
            } else {
                Toast.makeText(MainActivity.this, "蓝牙已开启", Toast.LENGTH_SHORT).show();
                return;
            }
        }
    }

    private class MySetShowListener implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            Intent discoverIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
            discoverIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
            startActivity(discoverIntent);
        }
    }

    public class BluetoothReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                BluetoothDevice scanDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                Log.d(TAG, "on3");
//                if (scanDevice.getBondState() != BluetoothDevice.BOND_BONDED) {
                    String scanName = scanDevice.getName();
                    String scanAddress = scanDevice.getAddress();
                    noPairBTM = new ArrayList<String>();
                    noPairBTM.add("已搜索到的蓝牙设备名字是：" + scanName + "，地址是：" + scanAddress + "。");
                    String s = "";
                    s +=  "已搜索到的蓝牙设备名字是：" + scanName + "，地址是：" + scanAddress;
                    Log.d(TAG, s);
//                }
            } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                mBluetoothAdapter.cancelDiscovery();
                Log.d(TAG, "123");
                ArrayAdapter<String> arrayNoList = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1, noPairBTM);
                noPairBTMSG.setAdapter(arrayNoList);
            }
        }
    }

    private class MyScanBT implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            mBluetoothAdapter.startDiscovery();
        }
    }
}
